//
//  StringExtensions.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/11.
//

import UIKit
import Guitar

extension String {
    var dic: [String: Any]? {
        guard let data = self.data(using: .utf8) else {return nil}
        return try! JSONSerialization.jsonObject(with: data) as? [String: Any]
    }
    
    var array: [[String: Any]]? {
        guard let data = self.data(using: .utf8) else {return nil}
        return try! JSONSerialization.jsonObject(with: data) as? [[String: Any]]
    }
    
    func toArray()->[String] {
        let jsonData:Data = self.data(using: .utf8)!
        let array = try?JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        if array != nil {
            return array as! Array
        }
        return array as! Array
    }
    
    var htmlImage: [String] {
        let result = Guitar(pattern: "<(img|IMG)(.*?)(/>|></img>|>)").evaluateForStrings(from: self)
        return result
    }
    
    func labelSize(size: CGSize, font: UIFont) -> CGSize {
        let dic = [NSAttributedString.Key.font: font]
        let string = self as NSString
        let rect = string.boundingRect(with: size, options: [.usesFontLeading, .usesLineFragmentOrigin], attributes: dic, context: nil)
        return rect.size
    }
    //根据生日算出年龄
    func getAge() ->Int {
        let dateFormater = DateFormatter()
        dateFormater.dateFormat = "yyyy-MM-dd"
        dateFormater.locale = NSLocale.current
        let date = dateFormater.date(from: self)
        guard let datee = date else {return 0 }
        return ageWithDateOfBirth(datee)
    }
    
    func ageWithDateOfBirth(_ date: Date) -> NSInteger {
        //=========获得出生的年月日
        var components1 = NSCalendar.current.component(.year, from: date as Date)
        let birthYear = components1.years
        components1 = NSCalendar.current.component(.month, from: date as Date)
        let birthMouth = components1.months
        components1 = NSCalendar.current.component(.day, from: date as Date)
        
        let birthDay = components1.days
        
        //==========获得当前的年月日
        var components2 = NSCalendar.current.component(.year, from: Date())
        
        let currentYear = components2.years
        components2 = NSCalendar.current.component(.month, from: Date())
        let currentMouth = components2.months
        components2 = NSCalendar.current.component(.day, from: Date())
        let currentDay = components2.days
        var age = Int(currentYear.year ?? 0) - Int(birthYear.year ?? 0)
        if ((currentMouth.month! > birthMouth.month!) || (currentMouth.month! == birthMouth.month! && currentDay.day! >= birthDay.day!)){
            age = age + 1
        }
        
        return age
    }
    
    func getFirstLetter() ->String{
        let mutableString = NSMutableString(string: self)
        //把汉字转为拼音
        CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false)
        //去掉拼音的音标
        CFStringTransform(mutableString, nil, kCFStringTransformStripDiacritics, false)
        let string = String(mutableString)
        //去掉空格
        let pinyin = string.replacingOccurrences(of: " ", with: "")
        let fristString = String(pinyin.prefix(1))
        return fristString.uppercased()
    }
}
